//
//  BIDAddPhotoViewII.m
//  EducationAssistant
//
//  Created by mal on 15/9/15.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import "BIDAddPhotoViewII.h"
#import "BIDViewWithImageAndCloseBtn.h"

@interface BIDAddPhotoViewII()<BIDViewWithImageAndCloseBtnDelegate>
{
    NSMutableArray *_imgViewArr;
    int row;
    NSTimer *_timer;
}
@end

@implementation BIDAddPhotoViewII
@synthesize delegate;
@synthesize imgArr;

- (void)awakeFromNib
{
    row = 0;
    _imgViewArr = [[NSMutableArray alloc] init];
    imgArr = [[NSMutableArray alloc] init];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        row = 0;
        _imgViewArr = [[NSMutableArray alloc] init];
        imgArr = [[NSMutableArray alloc] init];
        //
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        self.frame = CGRectMake(0, 0, screenSize.width, 106);
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)addPhoto:(UIImage *)img
{
    [self tellOtherToStopWobble];
    if(img)
    {
        [imgArr addObject:img];
    }
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat itemSpacing = 8;
    CGFloat lineSpacing = 8;
    CGFloat leftSpacing = 8;
    CGFloat topSpacing = 8;
    CGFloat bottomSpacing = 8;
    CGSize imgViewSize = CGSizeMake(90, 90);
    CGFloat posX = 0;
    CGFloat posY = 0;
    if(_imgViewArr.count>0)
    {
        UIView *lastView = _imgViewArr[_imgViewArr.count-1];
        posX = CGRectGetMaxX(lastView.frame)+itemSpacing;
        if(posX+imgViewSize.width>screenSize.width)
        {
            posX = leftSpacing;
            posY = CGRectGetMaxY(lastView.frame) + lineSpacing;
        }
        else
        {
            posY = CGRectGetMinY(lastView.frame);
        }
    }
    else
    {
        posX = leftSpacing;
        posY = topSpacing;
    }
    BIDViewWithImageAndCloseBtn *imgView = [[[NSBundle mainBundle] loadNibNamed:@"BIDViewWithImageAndCloseBtn" owner:self options:nil] lastObject];
    [imgView setBackgroundColor:[UIColor clearColor]];
    imgView.delegate = self;
    imgView.tag = _imgViewArr.count;
    imgView.frame = CGRectMake(posX, posY, imgViewSize.width, imgViewSize.height);
    imgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGR.numberOfTapsRequired = 1;
    tapGR.numberOfTouchesRequired = 1;
    [imgView addGestureRecognizer:tapGR];
    [_imgViewArr addObject:imgView];
    imgView.image = img;
    [self addSubview:imgView];
    //
    CGRect ownFrame = self.frame;
    ownFrame.size.height = CGRectGetMaxY(imgView.frame)+10 + bottomSpacing;
    self.frame = ownFrame;
    [delegate resizeViewWithFrame:ownFrame];
}

- (void)tapGestureHandler:(UITapGestureRecognizer*)gr
{
    UIView *view = gr.view;
    [delegate showPhotoAtIndex:view.tag];
}

- (void)animationDidStop:(NSString*)animation finished:(BOOL)finished context:(void*)context
{
    [imgArr removeObjectAtIndex:[(__bridge NSNumber*)context intValue]];
    [_imgViewArr removeObjectAtIndex:[(__bridge NSNumber*)context intValue]];
    CGRect ownFrame = self.frame;
    UIView *imgView = [_imgViewArr lastObject];
    ownFrame.size.height = CGRectGetMaxY(imgView.frame)+10 + 8;
    self.frame = ownFrame;
    [delegate resizeViewWithFrame:ownFrame];
    [self tellOtherToStopWobble];
}

#pragma mark -BIDViewWithImageAndCloseBtnDelegate
- (void)toDelete:(int)index
{
    NSArray *arr = [[NSArray alloc] initWithArray:_imgViewArr copyItems:YES];
    [UIView beginAnimations:@"" context:(__bridge void *)([NSNumber numberWithInt:index])];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    for(int i=index+1; i<_imgViewArr.count; i++)
    {
        UIView *view1 = _imgViewArr[i];
        UIView *frontView = arr[i-1];
        view1.frame = frontView.frame;
        view1.tag = frontView.tag;
    }
    [UIView commitAnimations];
}

- (void)tellOtherToWobble
{
    for(__strong UIView *customView in _imgViewArr)
    {
        [(BIDViewWithImageAndCloseBtn*)customView toWobble];
    }
}
- (void)tellOtherToStopWobble
{
    for(UIView *customView in _imgViewArr)
    {
        [(BIDViewWithImageAndCloseBtn*)customView toStop];
    }
}

@end
